/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CalculoDesignadoGui.java
 *
 * Created on Oct 29, 2011, 5:28:28 PM
 */
package manutençãoempregados.GUI;

import java.math.BigDecimal;
import javax.swing.JOptionPane;
import manutençãoempregados.UTILS.*;
import classesValidacao.ValidaDadosCalculo;
import classesDados.*;
import interfaces.IDadosCadastrais;

/**
 *
 * @author Leandro
 */
public class CalculoEmprestimoDesignadoGUI extends javax.swing.JDialog {
    
    /** Creates new form CalculoDesignadoGui */
    public CalculoEmprestimoDesignadoGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setFalseEnabled();
        setFalseEditable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cpfTxtField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        emprestimoTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        prestacoesTextField = new javax.swing.JTextField();
        calculaButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        printEmprestimo = new javax.swing.JTextField();
        printNome = new javax.swing.JTextField();
        printPrestacao = new javax.swing.JTextField();
        printTotal = new javax.swing.JTextField();
        SolicitaEmprestimo = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Cálculo Designado");

        cpfTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpfTxtFieldActionPerformed(evt);
            }
        });
        cpfTxtField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cpfTxtFieldFocusGained(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(0, 51, 51));
        jLabel2.setText("Encontre o cpf:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(0, 51, 51));
        jLabel3.setText("Valor do empréstimo:");

        emprestimoTextField.setText("entre com o valor do empréstimo");
        emprestimoTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emprestimoTextFieldActionPerformed(evt);
            }
        });
        emprestimoTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                emprestimoTextFieldFocusGained(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(0, 51, 51));
        jLabel4.setText("Número de prestações:");

        prestacoesTextField.setText("entre com o númeo de prestações");
        prestacoesTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prestacoesTextFieldActionPerformed(evt);
            }
        });
        prestacoesTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                prestacoesTextFieldFocusGained(evt);
            }
        });

        calculaButton.setText("Calcular");
        calculaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculaButtonActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(0, 51, 51));
        jLabel5.setText("Nome:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setForeground(new java.awt.Color(0, 51, 51));
        jLabel6.setText("Valor do Empréstimo:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setForeground(new java.awt.Color(0, 51, 51));
        jLabel7.setText("Valor de cada Prestação:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel8.setForeground(new java.awt.Color(0, 51, 51));
        jLabel8.setText("Valor total a pagar:");

        SolicitaEmprestimo.setText("Solicitar Empréstimo");
        SolicitaEmprestimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SolicitaEmprestimoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addContainerGap(247, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(SolicitaEmprestimo, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(calculaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(39, 39, 39)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel5))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(printNome, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                                        .addComponent(printEmprestimo, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                                        .addComponent(prestacoesTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel7)
                                        .addComponent(jLabel8))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(printTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                                        .addComponent(printPrestacao, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel2))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(cpfTxtField, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                                        .addComponent(emprestimoTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)))))))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cpfTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(emprestimoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(prestacoesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addComponent(calculaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(printNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(printEmprestimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(printPrestacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(printTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(SolicitaEmprestimo, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cpfTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpfTxtFieldActionPerformed
      try{
           if(cadEmpr.verificaExistencia(cpfTxtField.getText())){
               empregado = new Empregado(cadEmpr.getEmpregado(cpfTxtField.getText()));
               CPF = new CPF(cpfTxtField.getText());
               emprestimoTextField.setEnabled(true);
               emprestimoTextField.requestFocusInWindow();
              }
           else
               throw new Exception("Não foi encontrado");               
          }    
            catch(Exception ex){
             JOptionPane.showMessageDialog(this,ex.getMessage(), "CPF", JOptionPane.ERROR_MESSAGE);
       }     
}//GEN-LAST:event_cpfTxtFieldActionPerformed

private void emprestimoTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emprestimoTextFieldActionPerformed
    try {
        BigDecimal valor = new BigDecimal(emprestimoTextField.getText());
        if(ValidaDadosCalculo.validaEmprestimo(valor,empregado.getDatanasc(),empregado.getSexo(),empregado.getSalario())){
            
            valorEmprestimo = valor;
            prestacoesTextField.setEnabled(true);
            prestacoesTextField.requestFocusInWindow();
        }
        else
            throw new Exception("Valor indisponível");
        
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Prestacoes", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_emprestimoTextFieldActionPerformed

private void prestacoesTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prestacoesTextFieldActionPerformed
        
     try {
        if(ValidaDadosCalculo.validaPrestacoes(Integer.parseInt(prestacoesTextField.getText()))){
            prestacoes = (Integer.parseInt(prestacoesTextField.getText()));
            calculaButton.setEnabled(true);
            calculaButton.requestFocusInWindow();
        }
        else
            throw new Exception("O número deve ser maior que 1 e menor que 24");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Prestacoes", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_prestacoesTextFieldActionPerformed

private void calculaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calculaButtonActionPerformed
        CalcularEmrpestimoDesignado.CalcularEmprestimoDesignado(valorEmprestimo,prestacoes,empregado);
        setDados();
}//GEN-LAST:event_calculaButtonActionPerformed

private void cpfTxtFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cpfTxtFieldFocusGained
        cpfTxtField.setText("");
}//GEN-LAST:event_cpfTxtFieldFocusGained

private void emprestimoTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_emprestimoTextFieldFocusGained
        emprestimoTextField.setText("");
}//GEN-LAST:event_emprestimoTextFieldFocusGained

private void prestacoesTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_prestacoesTextFieldFocusGained
        prestacoesTextField.setText("");
}//GEN-LAST:event_prestacoesTextFieldFocusGained

private void SolicitaEmprestimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SolicitaEmprestimoActionPerformed
        IDadosCadastrais emprestimo = CalcularEmrpestimoDesignado.getEmprestimo();
        
    
        cadDesign.CadastrarObjeto(empregado);
}//GEN-LAST:event_SolicitaEmprestimoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CalculoEmprestimoDesignadoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CalculoEmprestimoDesignadoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CalculoEmprestimoDesignadoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CalculoEmprestimoDesignadoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                CalculoEmprestimoDesignadoGUI dialog = new CalculoEmprestimoDesignadoGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private void setFalseEditable(){
         printNome.setEditable(false); 
         printEmprestimo.setEditable(false);
         printPrestacao.setEditable(false);
         printTotal.setEditable(false);
    }
    
    private void setFalseEnabled() {
        emprestimoTextField.setEnabled(false);
        prestacoesTextField.setEnabled(false);
        calculaButton.setEnabled(false);
    }

     public void setDados() {
        printNome.setText(empregado.getNome());
        printEmprestimo.setText(String.valueOf(CalcularEmrpestimoDesignado.getValorEmprestimo()));
        printPrestacao.setText(CalcularEmrpestimoDesignado.getValorPrestacao().toString());
        printTotal.setText(CalcularEmrpestimoDesignado.getTotalPagar());
    }
    
    private Empregado empregado = new Empregado();
    private CadastrarEmpregado cadEmpr = new CadastrarEmpregado();
    private CadastrarEmprestimoDesignado cadDesign = new CadastrarEmprestimoDesignado();
    private CPF CPF;
    private int prestacoes;
    private BigDecimal valorEmprestimo;
    private double taxaJuros;
    private BigDecimal valorPrestacao;
    private String totalPagar;
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SolicitaEmprestimo;
    private javax.swing.JButton calculaButton;
    private javax.swing.JTextField cpfTxtField;
    private javax.swing.JTextField emprestimoTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField prestacoesTextField;
    private javax.swing.JTextField printEmprestimo;
    private javax.swing.JTextField printNome;
    private javax.swing.JTextField printPrestacao;
    private javax.swing.JTextField printTotal;
    // End of variables declaration//GEN-END:variables

}
